﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Obra.SmugMug.Api;

namespace Obra.PowerShell.SmugMug
{
    internal class AlbumCmdletServiceProvider :
        ICmdletService
    {
        private readonly PathSegments pathSegments;
        private readonly ISmugMugClientService smugMugClient;
        private readonly IPathMappingService pathMappingService;

        /// <summary>
        /// c-tor
        /// </summary>
        public AlbumCmdletServiceProvider(
            PathSegments pathSegments,
            ISmugMugClientService smugMugClient,
            PathMappingServiceProvider pathMappingService)
        {
            this.pathSegments = pathSegments;
            this.smugMugClient = smugMugClient;
            this.pathMappingService = pathMappingService;
        }

        #region ICmdletService Members

        /// <summary>
        /// User specified command line options.
        /// </summary>
        public DynamicParametersItem Options
        {
            get;
            set;
        }

        /// <summary>
        /// Name of the album.
        /// </summary>
        public string Name
        {
            get { return this.pathSegments.AlbumName; }
        }

        /// <summary>
        /// Fully qualifed path to the album.
        /// </summary>
        public string Path
        {
            get { return this.pathSegments.Path; }
        }

        public void Clear()
        {
            throw new NotImplementedException();
        }

        public void Copy(string path)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// True if the specified album exists.
        /// </summary>
        /// <returns></returns>
        public bool Exists()
        {
            return this.GetAlbum() != null;
        }

        public object Get()
        {
            return this.GetAlbum();
        }

        public Uri GetUri()
        {
            return new Uri(this.smugMugClient.GetImages(this.GetAlbum().Id).Images[0].AlbumUrl);
        }

        /// <summary>
        /// Get the images in this album.
        /// </summary>
        public DirectoryChild[] GetChildren()
        {
            var images = this.smugMugClient.GetImages(this.GetAlbum().Id).Images.Select(
                x => new Image(x, this.smugMugClient.Session, this.smugMugClient));

            return images.Select(x => new DirectoryChild(
                x.Name,
                //SmugMug.Path.Combine(this.pathSegments.AlbumPath, x.Name),
                System.IO.Path.Combine(this.pathSegments.AlbumPath, x.Name),
                x)).ToArray();
        }

        /// <summary>
        /// True, an album is a container
        /// </summary>
        public bool IsContainer()
        {
            return true;
        }

        public void Move(string path)
        {
            throw new NotImplementedException("Move");
        }

        public void New(string name, object value)
        {
            // TODO: get the category ID from the user.
            this.smugMugClient.CreateAlbum(name, 1);
        }

        public void Remove()
        {
            this.smugMugClient.DeleteAlbum(this.GetAlbum().Id);
        }

        public void Rename(string name)
        {
            this.smugMugClient.ChangeAlbumTitle(this.GetAlbum().Id, name);
        }

        public void Set(object value)
        {
            throw new NotImplementedException();
        }

        #endregion

        /// <summary>
        /// Get the one and only specified by the user.
        /// </summary>
        private Album GetAlbum()
        {
            //// User did not explicitly disambiguate the album.
            //if (this.dynamicParametersItem.Id == -1)
            //{
            //    return this.smugMugClient.GetAlbums().SingleOrDefault(
            //        x => x.Name == this.Name);
            //}

            AlbumContract albumContract = this.smugMugClient.GetAlbums().Albums.Single(
                x => x.Name == this.Name);

            return new Album(
                albumContract,
                this.smugMugClient.Session,
                this.smugMugClient);
        }
    }
}
